package indi.kider.sys.gateway.authc;

import indi.kider.common.model.employee.Employee;
import indi.kider.common.util.AuthcUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.vankid.common.constant.HttpCode;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.utils.weixin.WeixinDecryptUtil;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by shixj on 2018/3/15.
 */
@RestController
public class AuthcController {
    @Autowired
    private AuthcManager authcManager;

    /**
     * @api {POST} /login
     * @apiGroup authc
     * @apiDescription 登陆验证
     * @apiParamExample 请求样例：
     * http://localhost:9002/login
     * {"loginName":"shixj","pwd":"123456"}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"登陆成功！","data":{"id":1,"name":"史献建","phone":"18013897594","verification":"sss","email":"sss","cardId":"ss","cardimgBefore":"sss","cardimgback":"sss","position":"ss","origin":null,"nation":null,"ethnic":null,"education":null,"introduction":null,"entryTime":null,"pwd":"","title":null,"state":null,"enterpriseId":null,"roles":null,"lastLoginTime":null,"loginIp":null,"loginName":"shixj","sign":"rent-11577930701524618311594"}}
     * {"status":"400","msg":"用户密码不正确！","data":""}
     * {"status":"400","msg":"用户不存在！","data":""}
     * {"status":"401","msg":"请先登录系统！","data":""}
     */
    @PostMapping(value = "/login")
    public CommonResponse login(HttpServletRequest request, @RequestBody Employee user){
        return authcManager.submitLogin(user, request);
    }

    /**
     * @api {GET} /layout
     * @apiGroup authc
     * @apiDescription 注销登录
     * @apiParamExample 请求样例：
     * http://localhost:9002/layout
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"注销成功","data":""}
     */
    @RequestMapping(value = "/layout", method = RequestMethod.GET)
    public CommonResponse layout(HttpServletRequest request) throws Exception {
        //注销当前客户端已登录用户
        authcManager.layout(request);
        return new CommonResponse(HttpCode.SUCCESS_GET, "注销成功！", "");
    }

    /**
     * @api {GET} /current
     * @apiGroup authc
     * @apiDescription 获取用户ID
     * @apiParamExample 请求样例：
     * http://localhost:90002/current
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":"1"}
     */
    @GetMapping(value = "current")
    CommonResponse<Long> getCurrentUserId(HttpServletRequest request) {
        //查询当前客户端已登录用户
        return CommonResponse.buildGetSuccessResponse(authcManager.getCurrentUserId(AuthcUtil.getSignFromRequest(request)));
    }

    /**
     * @api {GET} /current
     * @apiGroup authc
     * @apiDescription 根据sign获取用户
     * @apiParamExample 请求样例：
     * http://localhost:90002/current/user
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":"1"}
     */
    @GetMapping(value = "current/user")
    CommonResponse<Employee> getCurrentUser(HttpServletRequest request) {
        //查询当前客户端已登录用户
        return CommonResponse.buildGetSuccessResponse(authcManager.getCurrentUser(request));
    }

    /**
     * 内部
     *
     * @param employee
     * @return
     */
    @PutMapping("user")
    CommonResponse updateLoginEmployee(HttpServletRequest request, @RequestBody Employee employee) {
        authcManager.updateUserJson(employee);
        return CommonResponse.buildUpdateSuccessResponse("");
    }

    @GetMapping("session-key/{code}")
    CommonResponse getSessionKey(@PathVariable String code) {
        return CommonResponse.buildGetSuccessResponse(
                WeixinDecryptUtil.getWxSessionBody(code, "wx38e7bcb5e4fd64a4", "f57efcfe91e4fd45c8c627009f059588"));
    }
}
